package com.cxc.news.controller;

import com.cxc.news.pojo.News;
import com.cxc.news.pojo.Type;
import com.cxc.news.pojo.User;
import com.cxc.news.service.NewsService;
import com.cxc.news.utils.Value;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/backstage/{userId}/{pagesNum}")
    public String toBackStage(@PathVariable("userId") Long userId,
                              @PathVariable("pagesNum") Integer pagesNum,
                              Map map){
        PageInfo<News> pageInfo = newsService.listNewsByUserId(pagesNum,5,userId);
        if (pageInfo!=null){
            map.put("pages",pageInfo);
        }
        return Value.PATH_BACKSTAGE;
    }

    /**
     * 浏览
     * @param newsId
     * @param map
     * @return
     */
    @GetMapping("/news/{newsId}")
    public String getNews(@PathVariable("newsId") Long newsId,
                          Map map){
        News news = newsService.getNews(newsId);
        if (news != null){
            map.put("news",news);
        }
        newsService.addViews(newsId);
        return Value.PATH_NEWS;
    }

    @PostMapping("/publish/{userId}")
    public String saveNews(News news,
                           @PathVariable("userId") Long userId,
                           String typeId){

        news.setType(new Type(Long.parseLong(typeId)));

        if (news.getContent() != null && news.getType()!=null && news.getTitle()!=null){
            news.setUpdateTime(new Date());
            news.setCreateTime(new Date());
            news.setViews(0);
            news.setUser(new User(userId));
            newsService.saveNews(news);
        }
        return "redirect:/backstage/"+userId+"/1";
    }

    /**
     * 删除
     * @param userId
     * @param newsId
     * @return
     */
    @RequestMapping("/delnews/{userId}/{newsId}")
    public String deleteNews(@PathVariable("userId") Long userId ,
                             @PathVariable("newsId") Long newsId){
        if (newsService.getNews(newsId) != null){
            newsService.deleteNews(newsId);
        }
        return "redirect:/backstage/"+userId+"/1";
    }

    @PostMapping("/publish-input/{userId}/{newsId}")
    public String updateNews(News news,
                             String typeId,
                             @PathVariable("userId") Long userId,
                             @PathVariable("newsId") Long newsId){

        if (typeId != null && news.getTitle()!=null && news.getContent()!=null){
            news.setId(newsId);
            news.setType(new Type(Long.parseLong(typeId)));
            news.setUpdateTime(new Date());
            news.setUser(new User(userId));
            newsService.updateNews(news);
        }
        return "redirect:/backstage/"+userId+"/1";
    }
}
